package com.service;

import com.dao.ActivityRepository;
import com.po.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService{

    @Autowired
    private ActivityRepository activityRepository;

    @Override
    public List<Activity> findAll() {
        return activityRepository.findAll();
    }

    public int findActivitySize() {
        return activityRepository.findAll().size();
    }

    @Override
    public List<Activity> findAllByState(int i) {
        return activityRepository.findAllByState(i);
    }

    @Override
    public Page<Activity> findAllByT(Pageable pageable) {
        return activityRepository.findAllNActivity(pageable);
    }

    @Override
    public Activity findById(Long id) {
        return activityRepository.findAllById(id);
    }

    @Override
    public void saveActivity(Activity a) {
        activityRepository.save(a);
    }

    @Override
    public List<Activity> findActivity(Long id) {
        return activityRepository.findActivityById(id);
    }

    @Override
    public int save(Long id,Long user_id) {
        return activityRepository.saveMangToMang(id,user_id);
    }

    @Override
    public Page<Activity> findAllByT1(Pageable pageable) {
        return activityRepository.findAll(pageable);
    }

    @Override
    public int delete(Long id, Long u_id) {
        return activityRepository.deleteM(id,u_id);
    }

    @Override
    public Activity getActivity(Long id) {
        return activityRepository.findById(id).get();
    }

    @Override
    public List<Activity> findAllByTypeId(Long id) {
        return activityRepository.findActivityByTypeId(id);
    }

    @Override
    public List<Activity> listRecommendActivityTop(Integer size) {
        Sort sort = Sort.by(Sort.Order.desc("activity_publish_time"));
        Pageable pageable = PageRequest.of(Integer.parseInt(String.valueOf(0)), Integer.parseInt(String.valueOf(size)), sort);
        return activityRepository.findTop(pageable);
    }

    @Override
    public Page<Activity> findAllByEnd(Pageable pageable) {
        return activityRepository.findAllByEnd(pageable);
    }


}
